
//
//  SingerViewController.swift
//  ShareMusic
//
//  Created by 胡岩 on 16/5/3.
//  Copyright © 2016年 胡岩. All rights reserved.
//

import UIKit

class SingerViewController: SMBViewController,UITableViewDelegate,UITableViewDataSource {
    var lists:NSMutableArray = NSMutableArray.init(capacity: 0)
    var tableView:UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.automaticallyAdjustsScrollViewInsets = false
        self.network()
        SMMusicPlayer.shareInstance()
        MusicList.shareInstance().close()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    
    func network() {
        Network.networkGet(URLs.singerUrl, parameters: nil) { (json) in
            if json["msg"] as! String == "ok" {
                let array = json["data"] as! NSArray
                for dic in array {
                    let model:SingerModel = SingerModel.init(dic: dic)
                    self.lists.addObject(model)
                }
            }
            self.initView()
        }
    }
    
    func initView() {
        self.tableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.Plain)
        self.view.addSubview(self.tableView!)
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.separatorStyle = UITableViewCellSeparatorStyle.None
        self.tableView?.registerClass(SingerTableViewCell.self, forCellReuseIdentifier: "cell")
        let swipe:UISwipeGestureRecognizer = UISwipeGestureRecognizer.init(target: self, action: #selector(self.swipeLeft(_:)))
        swipe.direction = UISwipeGestureRecognizerDirection.Left
        self.tableView?.addGestureRecognizer(swipe)
    }
    
    override func swipeLeft(swipe:UISwipeGestureRecognizer) {
        MusicList.shareInstance().open()
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return false
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:SingerTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SingerTableViewCell
        cell.model(self.lists[indexPath.row] as! SingerModel)
        return cell
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.lists.count
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 200
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        UIView.animateWithDuration(0.3, animations: {
            cell.transform = CGAffineTransformMakeScale(0.7, 1)
        }) { (finashed) in
            UIView.animateWithDuration(0.3, animations: {
                cell.transform = CGAffineTransformIdentity
            })
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let model:SingerModel = self.lists[indexPath.row] as! SingerModel
        //a=getnewttpod&id=48&page=1&size=1000
        let dic:[String:AnyObject] = ["a":"getnewttpod", "id":model.singerid!, "page":1, "size":1000]
        let singerDetails:SingerDetailsViewController = SingerDetailsViewController.init(parameters: dic)
        singerDetails.title = model.title
        singerDetails.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(singerDetails, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
